package fileReader.demo.reader;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.util.Vector;

public class PolygonDrawer extends LayerDrawer {

	protected Vector vertexList;
	protected double minDepth = 0;
	protected double maxDepth = 0;
	protected Vector holeList;
	
	public PolygonDrawer(){
		vertexList = new Vector();
	}

	/**
	 * Returns vertexList.
	 * @return vertexList
	 */
	public Vector getVertexList() {
		return vertexList;
	}

	/**
	 * Draws this polygon to the screen
	 * @param g graphics
	 * @param zoom value
	 * @param xtrans x_translation
	 * @param ytrans y_translation
	 * @param map 
	 * @param scale_factor scale
	 */
	public void Draw(Graphics g, double zoom, int xtrans, int ytrans, String layer) {
	
			Polygon p = new Polygon();
			double a,b; // xy-pixels on screen
			
			double minX=MapLayer.getGlobalMin_x();
	        double maxX=MapLayer.getGlobalMax_x();
	        double minY=MapLayer.getGlobalMin_y();
	        double maxY=MapLayer.getGlobalMax_y();
			
	        //scalex and scaley has to be the same for all the layers to get right proportions
			//scalex and scaley makes the map fit the screen
			double scalex = 1600.0/(maxX-minX);	//adapt this to your screen
			double scaley = 900.0/(minY-maxY);	//adapt this to your screen
	        
			for(int j = 0 ; j < vertexList.size() ; j++) 
			{
				Point2D cv = (Point2D) vertexList.elementAt(j); //Vector with the points on each Polygon
				//maps left top corners is set to left top corner on screen
				if(cv!=null)
				{
					a = ((cv.getX() - minX)*(scalex))*zoom+xtrans;
					b = ((cv.getY() - minY)*(scaley) +900.0)*zoom+ytrans;
					p.addPoint((int)a,(int)b);
				}
			}
			
			if(layer.contains("building")){
				g.setColor(Color.BLACK);
			    g.fillPolygon(p);
		        g.setColor(Color.BLACK);
		        g.drawPolygon(p);
			}else if(layer.contains("fwayarea")){
				g.setColor(new Color(217,255,204));	//light green
  			    g.fillPolygon(p);
			}else if(layer.contains("generarea")){
				g.setColor(new Color(253,246,168));	// light yellow
				g.fillPolygon(p); 
				g.setColor(Color.BLACK);
				g.drawPolygon(p);
			}else if(layer.contains("builtupare")){
				g.setColor(new Color(235,229,158)); //light yellow/brown
	            g.fillPolygon(p);
			}else if(layer.contains("deptharea")){
				if(minDepth >= Simulator.getSelectedDepth())
					g.setColor(Color.WHITE);
				else if(minDepth == 0.0 && maxDepth == 0.0)
					g.setColor(Color.RED);
				else
					g.setColor(new Color(228,250,255));
				
				g.fillPolygon(p);
				g.setColor(new Color(5,149,223));	//blue
				g.drawPolygon(p);
			}
	}


	public void reduceData(int n) {
		for(int i=0; i<vertexList.size(); i++)
	    {
	        if(((i+1)%n)!=0)
	        {
	        	boolean remove = true;
	        	if(((Point2D)vertexList.get(i)).getX()==boundingBox.getXMax() ||
	        			((Point2D)vertexList.get(i)).getX()==boundingBox.getXMin())
	                     	remove = false;
	            if(((Point2D)vertexList.get(i)).getY()==boundingBox.getYMax() ||
	            		((Point2D)vertexList.get(i)).getY()==boundingBox.getYMin())
	                        remove = false;
	
	            if(remove == true)
	            {
	            	vertexList.set(i, null);
	            }
	         }
	    }
		
		for(int k=vertexList.size()-1; k>=0; k--)
		{
			if(vertexList.get(k)==null)
			{
				vertexList.remove(k);
			}
		}
	}

	/**
	 * @return Returns the holeList.
	 */
	public Vector getHoleList() {
		return holeList;
	}

}
